package com.peixunp.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.peixunp.bean.Information;
import com.peixunp.bean.Organizations;
import com.peixunp.bean.Place;
import com.peixunp.bean.Remark;
import com.peixunp.service.InformationService;
import com.peixunp.service.OrganizationsService;
import com.peixunp.service.PlaceService;
import com.peixunp.service.RemarkService;
import com.peixunp.util.Constants;

import finaler.util.DateUtil;
import finaler.util.Page;
import finaler.util.PageList;

@Controller
@RequestMapping("school")
public class SchoolController {

	@Autowired
	private InformationService informationService;
	
	@Autowired
	private PlaceService placeService;

	@Autowired
	private OrganizationsService organizationsService;
	
	@Autowired
	private RemarkService remarkService;
	
	@RequestMapping( value="/{cityId}_{orgId}.html" )
	public String shool(@PathVariable int cityId,@PathVariable int orgId,HttpServletRequest request) throws Exception {
		Place city  = placeService.findByKey(cityId);//城市
		request.setAttribute("city", city);
		Organizations school = organizationsService.findByKey(orgId);//得到学校详细
		school.setHit( school.getHit( ) +1 );//浏览数更新
		organizationsService.update( school );
		request.setAttribute("school", school);
		List<Information> informationList  = informationService.getInformationList(orgId);//得到培训信息
		request.setAttribute("informationList", informationList);
		Page page = new Page(request);
		page.setRange(50);//每页50个评论
		page.putParameter("orgId", orgId);
		PageList remarkList = remarkService.getRemarkList(page);//得到评论集
		request.setAttribute("remarkList", remarkList);
		int avgFen = remarkService.getAvgFen(orgId);
		request.setAttribute("avgFen", avgFen);
		return "/school/school";
	}

	@RequestMapping( value="/remark",method=RequestMethod.POST )
	public String remark(HttpServletRequest request) throws Exception {
		Remark remark = new Remark();
		remark.setRmcontent(request.getParameter("rmcontent"));//内容
		remark.setAutoflag(0);//正常发
		remark.setCoin(Integer.parseInt(request.getParameter("coin")));
		remark.setOrgid(Integer.parseInt(request.getParameter("orgId")));
		remark.setCreatetime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
		remark.setCreateuser(request.getParameter("createuser"));
		remarkService.insert(remark);
		return "redirect:/school/"+request.getParameter("cityId")+"_"+request.getParameter("orgId")+".html"; 
	}

	@RequestMapping( value="/want_{orgId}",method=RequestMethod.POST )
    @ResponseBody
	public Object want(@PathVariable int orgId) throws Exception {
		Organizations school = organizationsService.findByKey(orgId);//得到学校详细
		school.setWant( school.getWant( ) +1 );//浏览数更新
		organizationsService.update( school );
		return "ok";
	}

	@RequestMapping( value="/once_{orgId}",method=RequestMethod.POST )
    @ResponseBody
	public Object once(@PathVariable int orgId) throws Exception {
		Organizations school = organizationsService.findByKey(orgId);//得到学校详细
		school.setOnce( school.getOnce( ) +1 );//浏览数更新
		organizationsService.update( school );
		return "ok";
	}

	@RequestMapping( value="/addInformation_{orgId}",method=RequestMethod.POST )
    @ResponseBody
	public Object addinformation(@PathVariable int orgId,HttpServletRequest request) throws Exception {
		Information information = new Information();
		information.setAuditFlag(0);
		information.setIfcontent(request.getParameter("ifcontent"));
		information.setOrgid(orgId);
		information.setCreatetime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
		informationService.insert(information);
		return "ok";
	}


	@RequestMapping( value="/add.html" )
	public String add() throws Exception {
		return "/school/add";
	}
	
	@RequestMapping( value="/save",method=RequestMethod.POST )
    @ResponseBody
	public void save(HttpServletRequest request,HttpServletResponse response) throws Exception {
		Organizations org = new Organizations();
		org.setAuditFlag(0);
		org.setCity(Integer.parseInt(request.getParameter("p_city")));
		org.setOrgaddr(request.getParameter("orgaddr"));
		org.setOrgcontent(request.getParameter("orgcontent"));
		org.setOrgname(request.getParameter("orgname"));
		org.setOrgtel(request.getParameter("orgtel"));
		organizationsService.insert(org);
		response.setContentType("text/html; charset=utf-8");
		response.getWriter().print("<script>alert('添加成功,请等待审核');window.location='"+Constants.URL+"'</script>");
	}
}
